﻿using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using TK.Common.Sample.ModelDTO;

namespace TK.Common.Sample.Json
{
    /// <summary>
    /// CustomTypeInfoResolver
    /// </summary>
    public class CustomTypeInfoResolver : DefaultJsonTypeInfoResolver
    {
        /// <summary>
        /// GetTypeInfo
        /// </summary>
        public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
        {
            var info = base.GetTypeInfo(type, options);
            if (info.Type == typeof(TestAddDto))
            {
                info.PolymorphismOptions = new JsonPolymorphismOptions
                {
                    TypeDiscriminatorPropertyName = typeof(TestAddDto).Name
                };
                info.PolymorphismOptions.DerivedTypes.Add(new JsonDerivedType(typeof(TestUpdateDto), typeof(TestUpdateDto).Name));
            }

            return info;
        }
    }
}
